package sword;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/6/8 14:35
 * https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/
 */
public class Title34 {

    class TreeNode{
        int val;

        TreeNode left;

        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        List<Integer> path = new ArrayList<>();
        dfs(root, result, path, target);
        return result;
    }

    private void dfs(TreeNode root, List<List<Integer>> result, List<Integer> path, int target) {
        path.add(root.val);
        target -= root.val;
        if (root.left == null && root.right == null && target == 0) {
            result.add(new ArrayList<>(path));
            return;
        }
        if (root.left != null) {
            dfs(root.left, result, path, target);
            path.remove(path.size() - 1);
        }
        if (root.right != null) {
            dfs(root.right, result, path, target);
            path.remove(path.size() - 1);
        }
    }

    public static void main(String[] args) {

    }
}
